//
//  PictureBrowserAnimator.swift
//  XinLangWeibo10
//
//  Created by 白利兵 on 15/11/8.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit
// 定义展现动画协议
protocol PictureAnimatorPresentDelegate: NSObjectProtocol {
    // 定义方法
    // 图片位置索引
    func imageViewForPresent(indexPath: NSIndexPath) -> UIImageView
    // 源图片的frame
    func photoBrowserPresentFromRect(indexPath: NSIndexPath) -> CGRect
    // 目标图片的frame
    func photoBrowserPresentToRect(indexPath: NSIndexPath) -> CGRect
}
// 定义解除动画协议
protocol PictureAnimatorDismissDelegate: NSObjectProtocol {
    /// 解除转场动画的图像视图
    func imageViewForDismiss() -> UIImageView
    /// 解除转场动画的图像索引
    func indexPathForDismiss() -> NSIndexPath
}
class PictureBrowserAnimator: NSObject, UIViewControllerTransitioningDelegate {
    // 定义代理属性
    weak var presentDelegate: PictureAnimatorPresentDelegate?
    weak var dismissDelegate: PictureAnimatorDismissDelegate?
    // 展现动画索引
    var indexPath: NSIndexPath?
    // 定义一个是否展现属性
    private var isPresented: Bool?
    /// 设置代理参数
    ///
    /// - parameter presentDelegate: 展现转场代理
    /// - parameter dismissDelegate: 解除转场代理
    /// - parameter indexPath:       照片索引
    func setDelegateParams(presentDelegate: PictureAnimatorPresentDelegate, dismissDelegate: PictureAnimatorDismissDelegate, indexPath: NSIndexPath) {
        self.presentDelegate = presentDelegate
        self.dismissDelegate = dismissDelegate
        self.indexPath = indexPath
    }
    // 设置转场动画对象
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning?{
        isPresented = true
        return self
    }
    
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = false
        return self
    }
}
extension PictureBrowserAnimator: UIViewControllerAnimatedTransitioning {
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval{
        return 0.5
    }
    // This method can only  be a nop if the transition is interactive and not a percentDriven interactive transition.
    func animateTransition(transitionContext: UIViewControllerContextTransitioning){
        // 获取转场动画上下文
//        // 获取源控制器
//        let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
//        // 获取目标控制器
//        let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
//        print(toVC)
//        // 获取源视图
//        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
//        print(fromView)
//        // 获取目标视图
//        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)
        // 判断是否展现动画
        if isPresented == true {
            transitionAnimationPresenting(transitionContext)
        }
        else {
            transitionAnimationDismissing(transitionContext)
        }
        
    }
    // 展现动画
    private func transitionAnimationPresenting(transitionContext: UIViewControllerContextTransitioning) {
        // 获取目标视图
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
        transitionContext.containerView()?.addSubview(toView)
        // 设置透明度，刚开始不显示
        toView.alpha = 0
        // 建立转场动画的头像视图
        guard let presentDelegate = self.presentDelegate, indexPath = self.indexPath else {
            return
        }
        // 创建转场图片
        let iv = presentDelegate.imageViewForPresent(indexPath)
        transitionContext.containerView()?.addSubview(iv)
        // 设置frame为原始小图
        iv.frame = presentDelegate.photoBrowserPresentFromRect(indexPath)
        // 设置动画效果
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: {
            // 设置透明度，并显示出来
            iv.alpha = 0
            // 把目标视图显示出来
            toView.alpha = 1.0
            // 获取视图的frame
            iv.frame = (self.presentDelegate?.photoBrowserPresentToRect(indexPath))!
            }) { _ in
                // 删除动画转场的图像视图
                iv.removeFromSuperview()
                // 动画完成后的回调
                transitionContext.completeTransition(true)
        }
    }
    // 消失动画
    private func transitionAnimationDismissing(transitionContext: UIViewControllerContextTransitioning) {
        // 获取源视图
        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
        guard let dismissDelegate = dismissDelegate else {
            return
        }
        // 获取动画图片
        let iv = dismissDelegate.imageViewForDismiss()
        // 获取源图片的索引
        let indexPath = dismissDelegate.indexPathForDismiss()
        // 把图片添加到父视图中
        transitionContext.containerView()?.addSubview(iv)
        // 设置动画效果
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            iv.frame = (self.presentDelegate?.photoBrowserPresentFromRect(indexPath))!
            // 源视图设为不可见
            fromView?.alpha = 0.0
            }) { (_) -> Void in
                // 移动画视图
                iv.removeFromSuperview()
                // 移除源视图
                fromView?.removeFromSuperview()
                // 完成转场动画的回调
                transitionContext.completeTransition(true)
        }
    }
    
}




















